﻿using System;
using System.Windows.Forms;
using QuickShot.Windows;

namespace QuickShot.Shortcut
{
    public sealed class GlobalHotkey : IDisposable
    {
        public Hotkey Hotkey { get; set; }

        private IntPtr hWnd = IntPtr.Zero;
        private int id = 0;


        public GlobalHotkey(Hotkey hotkey, IntPtr windowHandle)
        {
            Hotkey = hotkey;
            hWnd = windowHandle;
            id = GetHashCode();
        }

        public bool Register()
        {
            return WinAPI.RegisterHotKey(hWnd, id, (uint)Hotkey.Modifier, (uint)Hotkey.Key);
        }

        public bool Unregister()
        {
            return WinAPI.UnregisterHotKey(hWnd, id);
        }

        public override int GetHashCode()
        {
            return (int)Hotkey.Key ^ (int)Hotkey.Modifier ^ hWnd.ToInt32();
        }

        public void Dispose()
        {
            Unregister();
        }

    }

    public static class Constants
    {
        public const int WM_HOTKEY = 0x0312;
    }

    [Flags]
    public enum Modifiers : uint
    {
        None = 0x0000,
        Alt = 0x0001,
        Control = 0x0002,
        Shift = 0x0004,
        Win = 0x0008
    }
}
